﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Drop : MonoBehaviour
{

    public static Drop instance = null;
    [HideInInspector]
    public Image img;
    public Transform content;
    public GameObject pref;
    public Text weight;

    private void Awake()
    {
        instance = this;
        gameObject.SetActive(false);
        img = GetComponent<Image>();
    }

    public void HighLightColor()
    {
        img.color = new Color(1, 245.0f / 255.0f, 141.0f / 255.0f, 141.0f / 255.0f);
    }
    public void RedColor()
    {
        img.color = new Color(1, 6.0f / 255.0f, 17.0f / 255.0f, 186.0f / 255.0f);
    }

    public bool HasItem( Transform obj)
    {
        return dict_items.ContainsKey(obj.name);
    }

    public Dictionary<string, OutItem> dict_items = new Dictionary<string, OutItem>();
    public void OnDrop(Transform obj)
    {
        Debug.Log("drop >>>>>>>>>");
        AddItem(obj.name);


    }
    public void AddItem(string name)
    {
        if (!dict_items.ContainsKey(name))
        {
            var inst = GameObject.Instantiate(pref, content);
            var item = inst.GetComponent<OutItem>();
            item.img.sprite = Resources.Load<Sprite>($"items/{name}");
            item.slider.value = 0.1f;
            dict_items.Add(name, item);
            this.refreshWeight();
        }
    }

    public float sumWeight = 0.0f;
    public void refreshWeight()
    {
        float sum = 0;
        foreach( var key in dict_items.Keys)
        {
            var item = dict_items[key];
            sum += item.slider.value;
        }

        sumWeight = sum;
        weight.text = string.Format("总权重: {0:F2}", sum);
    }
    public void resetWeight()
    {
        weight.text = string.Format("总权重: {0:F2}", 0);
    }
}
